package com.xbongbong.paas.log.model.impl;

import com.xbongbong.paas.log.MongoLogUtil;
import com.xbongbong.paas.log.MongoQueryConditions;
import com.xbongbong.paas.log.entity.OperateDetailLogEntity;
import com.xbongbong.paas.log.entity.OperateLogEntity;
import com.xbongbong.paas.log.model.OperateLogModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 操作日志相关model方法实现
 * @author kaka
 * @date 2019/1/22 20:21
 * @since v1.0
 * @version v1.0
 */
@Service("operateLogModel")
public class OperateLogModelImpl implements OperateLogModel {

    private static final Logger LOG = LoggerFactory.getLogger(OperateLogModelImpl.class);

    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 保存操作日志
     * @param entity 操作日志实体
     */
    @Override
    @Async(value = "sysLogThreadPool")
    public Integer insert(OperateLogEntity entity) throws XbbException {
        try {
            mongoTemplate.save(entity);
        } catch (Exception e) {
            LOG.error("OperateLogModelImpl.insert 执行出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return 1;
    }

    /**
     * 保存操作日志
     * @param entity 操作日志实体
     */
    @Override
    public Integer insertWithoutAsync(OperateLogEntity entity) throws XbbException {
        try {
            mongoTemplate.save(entity);
        } catch (Exception e) {
            LOG.error("OperateLogModelImpl.insert 执行出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return 1;
    }

    @Override
    @Async(value = "sysLogThreadPool")
    public Integer insertIncludeDetail(OperateLogEntity entity, OperateDetailLogEntity operateDetailLogEntity) throws XbbException {
        try {
            //先插入日志
            mongoTemplate.save(entity);

            //再插入详情（把日志的id置入详情的operateLogId字段）
            operateDetailLogEntity.setCorpid(entity.getCorpid());
            operateDetailLogEntity.setOperateLogId(entity.getId());
            operateDetailLogEntity.setAddTime(entity.getAddTime());
            mongoTemplate.insert(operateDetailLogEntity);
        } catch (Exception e) {
            LOG.error("OperateLogModelImpl.insertIncludeDetail 执行出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return 1;
    }

    @Override
    public Integer insertIncludeDetailWithoutAsync(OperateLogEntity entity, OperateDetailLogEntity operateDetailLogEntity) throws XbbException {
        try {
            //先插入日志
            mongoTemplate.save(entity);

            //再插入详情（把日志的id置入详情的operateLogId字段）
            operateDetailLogEntity.setCorpid(entity.getCorpid());
            operateDetailLogEntity.setOperateLogId(entity.getId());
            operateDetailLogEntity.setAddTime(entity.getAddTime());
            mongoTemplate.insert(operateDetailLogEntity);
        } catch (Exception e) {
            LOG.error("OperateLogModelImpl.insertIncludeDetail 执行出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return 1;
    }

    @Override
    public Integer update(OperateLogEntity entity) throws XbbException {
        // 目前没有更新需求
        return null;
    }

    @Override
    public List findEntitys(Map<String, Object> param) {
       return findEntitys(param,false);
    }

    @Override
    public List findEntitys(Map<String, Object> param,boolean isExport) {
        MongoQueryConditions conditions = MongoLogUtil.formatOperateLogQueryConditions(param);
        Query query = MongoLogUtil.formatQuery(conditions,isExport);
        return mongoTemplate.find(query, OperateLogEntity.class);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        MongoQueryConditions conditions = MongoLogUtil.formatOperateLogQueryConditions(param);
        Query query = MongoLogUtil.formatQuery(conditions,false);
        return (int) mongoTemplate.count(query, OperateLogEntity.class);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

}
